package team.se.tms.controller.error_analysis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import team.se.tms.controller.BaseController;
import team.se.tms.dto.CourseLabelDetail;
import team.se.tms.dto.EchartsErrorAnalysisDetail;
import team.se.tms.dto.LabelNumberDetail;
import team.se.tms.po.LabelLibInfo;
import team.se.tms.po.TeachCourseInfo;
import team.se.tms.service.LabelLibInfoService;
import team.se.tms.service.TeachCourseInfoService;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("error_analysis")
public class ErrorAnalysisController extends BaseController{

    @Autowired
    TeachCourseInfoService teachCourseInfoService;
    @Autowired
    LabelLibInfoService labelLibInfoService;

    /**
     * 去错误分析页
     *
     * @return ModelAndView 错误分析页和数据
     * @throws Exception 异常
     * @create on 2019/1/3 by johnny
     */
    @RequestMapping("view")
    public ModelAndView toViewErrorAnalysis() throws Exception{

        ModelAndView mv = this.getModelAndView();
        List<LabelLibInfo> labelLibInfoList= labelLibInfoService.listAllLabelLibInfos(null);
        mv.addObject("labelLibInfoList",labelLibInfoList);
        mv.setViewName("error_analysis/errorAnalysis");
        return mv;
    }
    /**
     * 根据标签库id查询授课信息
     *
     * @param labelLibId 标签库
     * @return Object 授课信息集合
     * @throws Exception 异常
     * @create on 2019/1/3 by johnny
     */
    @RequestMapping("getTeachCourse")
    @ResponseBody
    public Object searchTeachCourseInfos(String labelLibId) throws Exception{
        List<TeachCourseInfo> teachCourseInfoList
                = teachCourseInfoService.listTeachCourseInfosByLabelLibId(labelLibId);
        return teachCourseInfoList;

    }

    /**
     * 查询授课对应的标签及其中的数量
     *
     * @param teachCourseIdList 授课信息列表
     * @return String 页面地址
     * @throws Exception 异常
     * @create on 2019/1/3 by johnny
     */
    @RequestMapping("getTeachCourseLabel")
    @ResponseBody
    public Object searchTeachCourseLabelDetail(@RequestParam("datas") List<String> teachCourseIdList) throws Exception{
        List<CourseLabelDetail>  courseLabelDetailList=teachCourseInfoService.listTeachCourseLabelDetails(teachCourseIdList);
        EchartsErrorAnalysisDetail echartsErrorAnalysisDetail=  this.getEchartsErrorAnalysisDetail(courseLabelDetailList);
        return echartsErrorAnalysisDetail;
    }

    public EchartsErrorAnalysisDetail getEchartsErrorAnalysisDetail(List<CourseLabelDetail>  courseLabelDetailList){
        Set<String> yAxisData =new HashSet<>();
        //获取y坐标的值：即所有标签（去重）后的集合
        for(CourseLabelDetail courseLabelDetail:courseLabelDetailList){
            List<LabelNumberDetail> labelNumberDetailList= courseLabelDetail.getLabelNumberDetailList();
            if(labelNumberDetailList!=null && labelNumberDetailList.size()>0){
                for(LabelNumberDetail labelNumberDetail:labelNumberDetailList){
                    yAxisData.add(labelNumberDetail.getLaeblName());
                }
            }
        }
//        List<List<Integer>> seriesData = new ArrayList<>();
//        //每一个标签
//        for(String labelName:yAxisData){
//            List<Integer> list = new ArrayList<>();
//            //每一个课程
//            for(CourseLabelDetail courseLabelDetail:courseLabelDetailList){
//                List<LabelNumberDetail> labelNumberDetailList= courseLabelDetail.getLabelNumberDetailList();
//                if(labelNumberDetailList==null ||labelNumberDetailList.size()==0){
//                    list.add(0);
//                    continue;
//                }
//
//                boolean flag =false;
//                //课程中的标签
//                for(LabelNumberDetail labelNumberDetail:labelNumberDetailList){
//                    if(labelName.equals(labelNumberDetail.getLaeblName())){
//                        list.add(labelNumberDetail.getLabelNumber());
//                        flag = true;
//                        break;
//                    }
//                }
//                //如果该课程没有该标签
//                if(flag==false){
//                    list.add(0);
//                }
//            }
//            seriesData.add(list);
//        }
        List<String> nameList= new ArrayList<>();
        List<List<Integer>> seriesData = new ArrayList<>();
        for(CourseLabelDetail courseLabelDetail:courseLabelDetailList){
            //课程名
            nameList.add(courseLabelDetail.getTeachCourseName());
            //每个课程对应的标签-数量信息
            List<LabelNumberDetail> labelNumberDetailList= courseLabelDetail.getLabelNumberDetailList();
            //该课程对每一个标签的数量
            List<Integer> list = new ArrayList<>();
            for(String labelName:yAxisData){
                boolean flag =false;
                for(LabelNumberDetail labelNumberDetail:labelNumberDetailList){
                    if(labelName.equals(labelNumberDetail.getLaeblName())){
                        list.add(labelNumberDetail.getLabelNumber());
                        flag = true;
                        break;
                    }
                }
                //如果该课程没有该标签
                if(flag==false){
                    list.add(0);
                }
            }
            seriesData.add(list);
        }
        //构造EchartsErrorAnalysisDetail
        EchartsErrorAnalysisDetail echartsErrorAnalysisDetail = new EchartsErrorAnalysisDetail();
        echartsErrorAnalysisDetail.setYAxisData(yAxisData);
        echartsErrorAnalysisDetail.setSeriesData(seriesData);
        echartsErrorAnalysisDetail.setNameList(nameList);
        return echartsErrorAnalysisDetail;
    }
}
